<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8">
    <title></title>
    <link rel="stylesheet" type="text/css" href="/js/ext/resources/css/ext-all.css"/>
    <link rel="stylesheet" type="text/css" href="/js/ext/resources/css/xtheme-gray.css"/>
    <link rel="stylesheet" type="text/css" href="/css/icon.css"/>
    <script type="text/javascript" src="/js/ext/adapter/ext/ext-base.js"></script>
    <script type="text/javascript" src="/js/ext/ext-all.js"></script>
    <script type="text/javascript" src="/js/ext/source/locale/ext-lang-zh_CN.js"></script>
    <style>
        .x-date-middle {
            padding-top:2px;padding-bottom:2px;
            width:130px; /* FF3 */
        }
    </style>
    <script type="text/javascript">
        Ext.onReady(function () {
            Ext.BLANK_IMAGE_URL = "/js/ext/resources/images/default/s.gif";
            Ext.QuickTips.init();
            Ext.form.Field.prototype.msgTarget = "qtip";//side:右侧 under:下方  id: [element id]错误提示显示在指定id的HTML元件中 qtip:鼠标移动到组件时显示
            var opUrl = "/project/addasShop";
            var delUrl = opUrl;
            var viewUrl = opUrl;
            var addurl = opUrl;
            var editUrl = opUrl;
            var dictUrl = "/getDict?code=";
            var loadMarsk = new Ext.LoadMask(document.body, {
                msg: '正在加载数据，请稍候......',
                removeMask: true// 完成后移除
            });
/////////////////////////字典匹配////////////////////////////////////////////////////
            function render_status(value) {
                if(value=='0'){
                    return "未激活";
                }
                if(value=='1'){
                    return "已激活";
                }
                if(value=='2'){
                    return "已注销";
                }
            }

///////////////////////////Store区///////////////////////////////////
            var store_grid = new Ext.data.JsonStore({
                url: viewUrl+'?op=view', //后缀名为json的文件
                totalProperty: "totalCount",
                root: 'rows',     //读取里面的users对象
                fields: ['shopName','address','managerUser','managerUserId','status','openDate','totalMoney','todyMoney','thisMonthMoney','age'] //读取里面的属性
            });
            var store_status = new Ext.data.JsonStore({
                url: dictUrl+'acyframework_users_status',
                root: '',
                fields: ['label', 'value']
            });

            //定义勾选框，不需要可不必定义
            var sm = new Ext.grid.CheckboxSelectionModel();
            //定义列,在这里定义每列的宽度
            var colM = new Ext.grid.ColumnModel([
                new Ext.grid.RowNumberer(),//增加自动编号，不需要可不必定义
                sm,//勾选框，不需要可不必定义
                {
                    header: '店铺名称',
                    dataIndex: 'shopName'
                },
                {
                    header: '地址',
                    dataIndex: 'address'
                },
                {
                    header: '管理员',
                    dataIndex: 'managerUser'
                },
                {
                    header: '状态',
                        renderer: render_status,
                    dataIndex: 'status'
                },
                {
                    header: '营业时间',
                    dataIndex: 'openDate'
                },
                {
                    header: '总营业额',
                    dataIndex: 'totalMoney'
                },
                {
                    header: '今日营业额',
                    dataIndex: 'todyMoney'
                },
                {
                    header: '本月营业额',
                    dataIndex: 'thisMonthMoney'
                },
                {
                    header: '店铺年龄',
                    dataIndex: 'age'
                },
                {
                    header: 'id',
                    hidden:true,
                    dataIndex: 'id'
                }
            ]);

            //生成表格
            var grid = new Ext.grid.GridPanel({
                loadMask: {
                    msg: '数据加载中，请稍候...'
                },
                cm: colM,
                sm: sm,
                store: store_grid,
                viewConfig: {
                    forceFit: true
                },
                tbar: [
                    {
                        text: "添加",
                        iconCls: "add",
                        handler: function (e) {
                            add();
                        }
                    },
                    {
                        text: "修改",
                        iconCls: "pageedit",
                        handler: function (e) {
                            edit();
                        }
                    },
                    {
                        text: "删除",
                        iconCls: "delete",
                        handler:function(e){
                            del();
                        }
                    },
                    {
                        text: "刷新",
                        iconCls: "arrowrefresh",
                        handler:function(e){
                            store_grid.reload();
                        }
                    }
                ],
                bbar: new Ext.PagingToolbar({
                    pageSize: 3,
                    store: store_grid,
                    displayInfo: true,
                    displayMsg: '显示第{0}条到{1}条记录,一共{2}条',
                    emptyMsg: '没有记录'
                })
            });
            grid.on("rowdblclick", function (datagrid, row) {
                edit();
            });
            grid.on("rowcontextmenu", function (grid, rowIndex, e) {
                if (rowIndex < 0) {
                    return;
                }
                e.preventDefault();
                var allowEdit = true;
                var selArr = grid.getSelectionModel().getSelections();
                if (selArr.length <= 1) {
                    //选中行
                    grid.getSelectionModel().selectRow(rowIndex,false);
                } else if (selArr.length > 1) {
                    allowEdit = false;
                }

                var menus = new Ext.menu.Menu
                ({
                    items: [
                        {
                            text: "添加",
                            pressed: true,
                            iconCls: "add",
                            handler: function () {
                                add();
                                menus.destroy();
                            }
                        },
                        {
                            text: "编辑",
                            pressed: true,
                            disabled:!allowEdit,
                            iconCls: "pageedit",
                            handler: function () {
                                edit();
                                menus.destroy();
                            }
                        },
                        {
                            text: "删除",
                            pressed: true,
                            iconCls: "delete",
                            handler: function () {
                                del();
                                menus.destroy();
                            }
                        },
                        {
                            text: "刷新",
                            pressed: true,
                            iconCls: "arrowrefresh",
                            handler: function () {
                                store_grid.reload();
                            }
                        }
                    ]
                });
                menus.showAt(e.getPoint());
            });
            var viewport = new Ext.Viewport({
                layout: 'border',
                autoScroll: true,
                items: [{
                    region: 'center',
                    deferredRender: false,
                    bodyStyle: "overflow-y:hidden;overflow-x:auto",
                    items: grid,
                    autoScroll: true
                }]
            });
//////////////////////查询界面/////////////////////
            var query_form = new Ext.FormPanel({
                renderTo: grid.tbar,
                labelAlign: 'left',
                buttonAlign: 'right',
                border: false,
                frame: true,
                items: [
                    {
                        layout: 'table',
                        layoutConfig: {
                            columns: 4
                        },
                        border: false,
                        labelSeparator: ':',
                        defaults: {
                            layout: 'form',
                            labelWidth: 12 * 5,
                            bodyStyle: 'padding:0 0 0 20',
                            border: false
                        },
                        items: [

                                        {
                                            items: [
                                                {
                                                    xtype: 'field',
                                                    maxlength: 15,
                                                    minLength: 5,
                                                    name: "addasShop.shopName",
                                                    labelStyle : "text-align:right;width: 60;",
                                                    fieldLabel: "店铺名称"
                                                }
                                            ]
                                        },
                                        {
                                            items: [
                                                {
                                                    xtype: 'field',
                                                    maxlength: 15,
                                                    minLength: 5,
                                                    name: "addasShop.openDate",
                                                    labelStyle : "text-align:right;width: 60;",
                                                    fieldLabel: "营业时间"
                                                }
                                            ]
                                        },
                             {
                                layout: "table",
                                items: [
                                    {
                                        xtype: "button",
                                        text: "查询",
                                        iconCls: "find",
                                        handler: function () {
                                            //表单提交
                                            query(query_form);
                                        }
                                    }, {
                                        xtype: "button",
                                        text: "重置",
                                        iconCls: "erase",
                                        handler: function () {
                                            //表单提交
                                            query_form.form.reset();
                                            query(query_form);
                                        }
                                    },
                                    {
                                        xtype: 'hidden',
                                        name: "op",
                                        value: "view",
                                        fieldLabel: 'op'
                                    }
                                ]
                            }
                        ]
                    }
                ]//items
            })//FormPanel

            grid.setHeight(Ext.get("content").getHeight() - 5);
            store_grid.load();

//////////////////////编辑界面//////////////////////////////////////////
            function edit() {
                var selArr = grid.getSelectionModel().getSelections();
                if (selArr.length < 1) {
                    Ext.MessageBox.alert("", "请选择要编辑或修改的数据行!");
                    return;
                } else if (selArr.length > 1) {
                    Ext.MessageBox.alert("", "编辑或修改数据的时候不能多选!");
                    return;
                }
                json = selArr[0].json;
                var p = new Ext.FormPanel
                ({
                    frame: true, labelWidth: 12 * 6,
                    items: [
                                {
                                    xtype: "textfield",
                                    fieldLabel: "店铺名称",
                                    labelStyle : "text-align:right;width: 72;",
                                    id: "shopName",
                                    hiddenName: "addasShop.shopName",
                                    value:json.shopName,
                                    width: 201
                                },
                                {
                                    xtype: "textfield",
                                    fieldLabel: "地址",
                                    labelStyle : "text-align:right;width: 72;",
                                    id: "address",
                                    hiddenName: "addasShop.address",
                                    allowBlank: false,
                                    blankText: "不能为空，请填写",
                                    value:json.address,
                                    width: 201
                                },
                                {
                                    xtype: "textfield",
                                    fieldLabel: "管理员",
                                    labelStyle : "text-align:right;width: 72;",
                                    id: "managerUser",
                                    hiddenName: "addasShop.managerUser",
                                    value:json.managerUser,
                                    width: 201
                                },
                                {
                                    xtype: "combo",
                                    fieldLabel: "状态",
                                    labelStyle : "text-align:right;width: 72;",
                                    id: "status",
                                    hiddenName: "addasShop.status",
                                    name: "status",
                                    allowBlank: true,
                                    blankText: "不能为空，请填写",
                                    valueField: "value",
                                    displayField: "label",
                                    emptyText: "请选择...",
                                    mode: 'remote',
                                    forceSelection: true,
                                    triggerAction: 'all',
                                    width: 70,
                                    store: store_status
                                },
                                {
                                    xtype: "textfield",
                                    fieldLabel: "营业时间",
                                    labelStyle : "text-align:right;width: 72;",
                                    id: "openDate",
                                    hiddenName: "addasShop.openDate",
                                    allowBlank: false,
                                    blankText: "不能为空，请填写",
                                    value:json.openDate,
                                    width: 201
                                },
                                {
                                    xtype: "textfield",
                                    fieldLabel: "总营业额",
                                    labelStyle : "text-align:right;width: 72;",
                                    id: "totalMoney",
                                    hiddenName: "addasShop.totalMoney",
                                    regex:/^[0-9]+(\.[0-9]+){0,1}$/,
                                            regexText:'只能为数值型数据!',
                                    value:json.totalMoney,
                                    width: 201
                                },
                                {
                                    xtype: "textfield",
                                    fieldLabel: "今日营业额",
                                    labelStyle : "text-align:right;width: 72;",
                                    id: "todyMoney",
                                    hiddenName: "addasShop.todyMoney",
                                    regex:/^[0-9]+(\.[0-9]+){0,1}$/,
                                            regexText:'只能为数值型数据!',
                                    value:json.todyMoney,
                                    width: 201
                                },
                                {
                                    xtype: "textfield",
                                    fieldLabel: "本月营业额",
                                    labelStyle : "text-align:right;width: 72;",
                                    id: "thisMonthMoney",
                                    hiddenName: "addasShop.thisMonthMoney",
                                    regex:/^[0-9]+(\.[0-9]+){0,1}$/,
                                            regexText:'只能为数值型数据!',
                                    value:json.thisMonthMoney,
                                    width: 201
                                },
                                {
                                    xtype: "textfield",
                                    fieldLabel: "店铺年龄",
                                    labelStyle : "text-align:right;width: 72;",
                                    id: "age",
                                    hiddenName: "addasShop.age",
                                    regex:/^[0-9]*$/,
                                            regexText:'只能为数字!',
                                    value:json.age,
                                    width: 201
                                },
                        {
                            xtype: "hidden",
                            id: "id",
                            name: "addasShop.id",
                            width: 201,
                            value: json.id
                        },
                        {
                            xtype: "hidden",
                            id: "op",
                            name: "op",
                            width: 201,
                            value: "edit"
                        }
                    ]
                });
                store_status.on("load", function () {
                    comb = Ext.getCmp("status");
                    comb.setValue(json.status);
                });
                store_status.load();
                var win = new Ext.Window
                ({
                    title: "编辑窗口",
                    autoHeight: true,
                    width: 330,
                    resizable: false,
                    buttonAlign: "center",
                    modal: true,//height:300,
                    items: [p],
                    bbar: [
                        {
                            xtype: "button",
                            text: "确定",
                            iconCls: "accept",
                            handler: function () {
                                //表单提交
                                save(p, win,"edit");
                            }
                        },
                        '', '',
                        {
                            xtype: "button",
                            text: "关闭",
                            iconCls: "cancel",
                            handler: function () {
                                win.destroy();
                            }
                        }
                    ]
                });
                win.show();
            }

//////////////////////添加界面//////////////////////////////////////////
            function add() {
                var p = new Ext.FormPanel
                ({
                    frame: true,
                    labelWidth: 12 * 6,
                    items: [
                            {
                                xtype: "textfield",
                                fieldLabel: "店铺名称",
                                labelStyle : "text-align:right;width: 72;",
                                id: "shopName",
                                name: "addasShop.shopName",
                                width: 201
                            },
                            {
                                xtype: "textfield",
                                fieldLabel: "地址",
                                labelStyle : "text-align:right;width: 72;",
                                id: "address",
                                name: "addasShop.address",
                                    allowBlank: false,
                                    blankText: "不能为空，请填写",
                                width: 201
                            },
                            {
                                xtype: "textfield",
                                fieldLabel: "管理员",
                                labelStyle : "text-align:right;width: 72;",
                                id: "managerUser",
                                name: "addasShop.managerUser",
                                width: 201
                            },
                        {
                            xtype: "combo",
                            fieldLabel: "状态",
                            labelStyle : "text-align:right;width: 72;",
                            id: "status",
                            hiddenName: "addasShop.status",
                            name: "status",
                            allowBlank: true,
                            blankText: "不能为空，请填写",
                            valueField: "value",
                            displayField: "label",
                            emptyText: "请选择...",
                            mode: 'remote',
                            forceSelection: true,
                            triggerAction: 'all',
                            width: 70,
                            store: store_status
                        },
                            {
                                xtype: "textfield",
                                fieldLabel: "营业时间",
                                labelStyle : "text-align:right;width: 72;",
                                id: "openDate",
                                name: "addasShop.openDate",
                                    allowBlank: false,
                                    blankText: "不能为空，请填写",
                                width: 201
                            },
                            {
                                xtype: "textfield",
                                fieldLabel: "总营业额",
                                labelStyle : "text-align:right;width: 72;",
                                id: "totalMoney",
                                name: "addasShop.totalMoney",
                                    regex:/^[0-9]+(\.[0-9]+){0,1}$/,
                                    regexText:'只能为数值型数据!',
                                width: 201
                            },
                            {
                                xtype: "textfield",
                                fieldLabel: "今日营业额",
                                labelStyle : "text-align:right;width: 72;",
                                id: "todyMoney",
                                name: "addasShop.todyMoney",
                                    regex:/^[0-9]+(\.[0-9]+){0,1}$/,
                                    regexText:'只能为数值型数据!',
                                width: 201
                            },
                            {
                                xtype: "textfield",
                                fieldLabel: "本月营业额",
                                labelStyle : "text-align:right;width: 72;",
                                id: "thisMonthMoney",
                                name: "addasShop.thisMonthMoney",
                                    regex:/^[0-9]+(\.[0-9]+){0,1}$/,
                                    regexText:'只能为数值型数据!',
                                width: 201
                            },
                            {
                                xtype: "textfield",
                                fieldLabel: "店铺年龄",
                                labelStyle : "text-align:right;width: 72;",
                                id: "age",
                                name: "addasShop.age",
                                    regex:/^[0-9]*$/,
                                    regexText:'只能为数字!',
                                width: 201
                            },
                        {
                            xtype: "hidden",
                            id: "op",
                            name: "op",
                            width: 201,
                            value: "add"
                        }
                    ]
                });

                var win = new Ext.Window
                ({
                    title: "新增窗口",
                    autoHeight: true,
                    width: 320,
                    resizable: false,
                    buttonAlign: "center",
                    modal: true,//height:300,
                    items: [p],
                    bbar: [
                        {
                            xtype: "button",
                            text: "确定",
                            iconCls: "accept",
                            handler: function () {
                                //表单提交
                                save(p, win,"add");
                            }
                        },
                        '', '',
                        {
                            xtype: "button",
                            text: "关闭",
                            iconCls: "cancel",
                            handler: function () {
                                win.destroy();
                            }
                        }
                    ]
                });
                win.show();
            }

            function save(aquForm, aquWin,op) {
                //验证合法后使用加载进度条
                if (aquForm.form.isValid()) {
                    //提交到服务器操作
                    aquForm.form.submit({
                        waitMsg: '操作进行中,请稍后...',
                        url: op=="edit"?editUrl:addurl,
                        method: 'post',
                        //提交成功的回调函数
                        success: function (form, action) {
                            if (action.result.errcode == 0) {
                                store_grid.reload();
                                aquWin.destroy();
                            } else {
                                Ext.Msg.alert("", action.result.errmsg);
                            }
                        },
                        //提交失败的回调函数
                        failure: function (form, action) {
                            switch (action.failureType) {
                                case Ext.form.Action.CLIENT_INVALID:
                                    Ext.Msg.alert('错误提示', '表单数据非法请核实后重新输入！');
                                    break;
                                case Ext.form.Action.CONNECT_FAILURE:
                                    Ext.Msg.alert('错误提示', '网络连接异常！');
                                    break;
                                case Ext.form.Action.SERVER_INVALID:
                                    Ext.Msg.alert('错误提示', "您的输入用户信息有误，请核实后重新输入！");
                                    simple.form.reset();
                            }
                        }
                    });
                }
            };
            function query(aquForm) {
                //验证合法后使用加载进度条
                if (aquForm.form.isValid()) {
                    //提交到服务器操作
                    store_grid.baseParams = query_form.form.getValues();
                    aquForm.form.submit({
                        waitMsg: '操作进行中,请稍后...',
                        url: viewUrl,
                        method: 'post',
                        //提交成功的回调函数
                        success: function (form, action) {
                            if (action.result.errcode == 0) {
                                store_grid.loadData(action.result);
                            } else {
                                Ext.Msg.alert("", action.result.errmsg);
                            }
                        },
                        //提交失败的回调函数
                        failure: function (form, action) {
                            switch (action.failureType) {
                                case Ext.form.Action.CLIENT_INVALID:
                                    Ext.Msg.alert('错误提示', '表单数据非法请核实后重新输入！');
                                    break;
                                case Ext.form.Action.CONNECT_FAILURE:
                                    Ext.Msg.alert('错误提示', '网络连接异常！');
                                    break;
                                case Ext.form.Action.SERVER_INVALID:
                                    Ext.Msg.alert('错误提示', "您的输入用户信息有误，请核实后重新输入！");
                            }
                        }
                    });
                }
            };
            function del(){
                var selModel = grid.getSelectionModel();
                if (selModel.hasSelection()) {
                    Ext.Msg.confirm("警告", "确定要删除吗？", function (button) {
                        if (button == "yes") {
                            var selected = grid.getSelectionModel().getSelections();
                            var ids = []; //要删除的id
                            Ext.each(selected, function (item) {
                                ids.push(item.json.id);
                            });
                            loadMarsk.show();
                            Ext.Ajax.request({
                                url: delUrl+'?op=delete&ids=' + ids,
                                //根据id删除节点
                                success: function (request) {
                                    store_grid.reload();
                                    loadMarsk.hide();
                                },
                                failure: function () {
                                    alert("删除失败");
                                    loadMarsk.hide();
                                }
                            });
                        }
                    });
                }
                else {
                    Ext.Msg.alert("错误", "没有任何行被选中，无法进行删除操作！");
                }
            }
        });
    </script>
</head>
<body>
<div id="content" style="width: 100%;height: 100%"></div>
</body>
</html>